package a51qindian.com.comprehensive.activity;

import android.widget.Button;
import android.widget.EditText;

import com.li.widget.MultipleLayout;
import com.li.widget.TopNavigationBar;

import java.util.HashMap;
import java.util.Map;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.base.QDBaseActivity;
import a51qindian.com.comprehensive.model.AddShopResult;
import a51qindian.com.comprehensive.model.BaseModel;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import a51qindian.com.comprehensive.net.SelectorObserver;
import a51qindian.com.comprehensive.util.SpUtil;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;


/**
 * Author: Li
 * Date: 2017/10/20 0020
 * Description: 添加商铺界面
 */

public class CityAddShopActivity extends QDBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.et_name)
    EditText mEtName;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.et_pwd_a)
    EditText mEtPwdA;
    @BindView(R.id.et_shop_name)
    EditText mEtShopName;
    @BindView(R.id.btn_confirm)
    Button mBtnConfirm;
    @BindView(R.id.layout_content)
    MultipleLayout mLayoutContent;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_city_add_shop;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
    }

    /**
     * 添加商铺
     *
     * @param uid      用户编号
     * @param userName 用户名
     * @param pwd      密码
     * @param pwdA     确认密码
     * @param shopName 商铺名
     */
    private void addShop(String uid, String userName, String pwd, String pwdA, String shopName) {
        mLayoutContent.onLoadingWithContent("正在创建");

        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("username", userName);
        map.put("password", pwd);
        map.put("repassword", pwdA);
        map.put("town", shopName);
        ObservableHelper.createObservable(RetrofitCreator.createService().addShop(map))
                .subscribe(new SelectorObserver<BaseModel<AddShopResult>>() {
                    @Override
                    protected void onErrorCodeInfo(int code, String info) {
                        mLayoutContent.onSuccess();
                        showToast(info);
                    }

                    @Override
                    protected void onSuccess(BaseModel<AddShopResult> stringBaseModel) {
                        mLayoutContent.onSuccess();
                        finish();
                        showToast("商铺已经添加成功");
                    }

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                    }
                });
    }


    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {

        if (checkIsEmpty(mEtName)||checkIsEmpty(mEtPwd)||checkIsEmpty(mEtPwdA)||checkIsEmpty(mEtShopName)) {
            showToast("请正确填写全部信息");
            return;
        }
        if (!mEtPwd.getText().toString().equals(mEtPwdA.getText().toString())) {
            showToast("两次密码输入不一样");
            return;
        }

        addShop(SpUtil.getUserInfo(CityAddShopActivity.this, SpUtil.USER_ID),
                mEtName.getText().toString(),
                mEtPwd.getText().toString(),
                mEtPwdA.getText().toString(),
                mEtShopName.getText().toString());
    }
}
